/************************************************************************/
/*                                                                      */
/************************************************************************/

#ifndef __X_QUEEN_H__
#define __X_QUEEN_H__

#include "core.h"
#include <boost/make_shared.hpp>
#include <vector>

template<class T> class CWnQueue : public std::vector<T>
{
public:
	CWnQueue()
	{
		m_ptr_mutex = boost::make_shared<CWnLock>();
	}
	~CWnQueue()
	{
	}

public:
	void push(T& t)
	{
		CWnScopedLock lock(*m_ptr_mutex);
		__super::push_back(t);
	}

	bool get(T& t)
	{
		CWnScopedLock lock(*m_ptr_mutex);
		if(!__super::empty())
		{
			t = __super::operator [](0);
			__super::erase(__super::begin()); 
			return true;
		}
		return false;
	}

	void clear()
	{
		CWnScopedLock lock(*m_ptr_mutex);
		__super::clear();
	}

protected:
	boost::shared_ptr<CWnLock> m_ptr_mutex;
};

template <class T> class CDataHandler
{
public:
	CDataHandler(void) {}
	virtual ~CDataHandler() {}

public:
	void NotifyOne() {m_event.notify_one();}

	void NotifyAll() {m_event.notify_all();}

	void Put(T& t)
	{
		m_record_set.push(t);
		m_event.notify_one();
	}

	int BufferSize() { return m_record_set.size(); }

	void Start()
	{
		boost::thread t(&CDataHandler::DataThread, this);
	}

protected:
	virtual void DataThread()
	{
		while(true)
		{
			m_event.wait();
			while(!m_record_set.empty())
			{
				T t;
				if(m_record_set.get(t))
				{
					DataFunc(t);
				}
			}
		}
	}

	virtual void DataFunc(T& t) {}

protected:
	CWnQueue<T> m_record_set;
	CWnEvent m_event;	
};

#endif //__X_QUEEN_H__